package com.bkck.sharkblockade.highscores;

import com.bkck.sharkblockade.levels.thread.MainThread;
import com.bkck.sharkblockade.menu.R;
import com.bkck.sharkblockade.menu.Menu;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * The Highscores page.
 * 
 * @author Team BKCK.
 * 
 */
public class Highscores extends Activity {

  private Spinner levels;
  String listOfScores = "";
  int levelToDisplay = 1;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // requesting to turn the title OFF
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // making it full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.highscores);

    levels = (Spinner) findViewById(R.id.levels);
    ArrayAdapter<CharSequence> adapter =
        ArrayAdapter.createFromResource(this, R.array.levels, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    levels.setAdapter(adapter);
    levels.setOnItemSelectedListener(new MyOnItemSelectedListener());
    
    displayLevelScores();
  }
  
  void displayLevelScores() {
	// Set Text view to hold high score list
    TextView tv = (TextView) findViewById(R.id.high_score_content);
    listOfScores = MainThread.getDB().listToString(MainThread.getDB().getScoresForLevel(levelToDisplay));
    
    if (listOfScores.equalsIgnoreCase("")) {
      tv.setText("\nNo Scores Yet");
    }
    else {
      tv.setText(listOfScores);
    }
  }

  /**
   * Switch to the activity for menu.
   * 
   * @param view The view.
   */
  public void toMenu(View view) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName(this, Menu.class.getName());
    startActivity(intent);
  }

  @Override
  protected void onStop() {
    finish();
    super.onDestroy();
  }

  /**
   * Ignore screen orientation change.
   */
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
  }

  /**
   * The listener for the level choices.
   * 
   * @author Team BKCK
   * 
   */
  public class MyOnItemSelectedListener implements OnItemSelectedListener {

    //@Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
      // change view content for high score
      switch (pos) {
      case 0:
    	  levelToDisplay = 1;
    	  break;
      case 1:
    	  levelToDisplay = 2;
    	  break;
      case 2:
    	  levelToDisplay = 3;
    	  break;
      case 3:
    	  levelToDisplay = 4;
    	  break;
      case 4:
    	  levelToDisplay = 5;
    	  break;
      default:
    	  break;    		  
      } // end switch
      displayLevelScores();

    }
    

    //@Override
    public void onNothingSelected(AdapterView<?> parent) {
      // Do nothing.
    }
  }
}